package example.avro.without_code_generation

import java.io.File

import org.apache.avro.Schema
import org.apache.avro.file.DataFileWriter
import org.apache.avro.generic.{GenericData, GenericDatumWriter, GenericRecord}
import org.apache.avro.io.DatumWriter

object SerializingDemo extends App {
  val schema: Schema = new Schema.Parser().parse(getClass.getClassLoader.getResourceAsStream("user.avsc"))
  val user1: GenericRecord = new GenericData.Record(schema)
  user1.put("name", "Hunter")
  user1.put("favorite_number", 256)

  val user2: GenericRecord = new GenericData.Record(schema)
  user2.put("name", "Mary")
  user2.put("favorite_number", 256)
  user2.put("favorite_color", "red")


  val file:File = new File("users_sample_without_generation.avro")
  val datumWriter:DatumWriter[GenericRecord] = new GenericDatumWriter[GenericRecord](schema)
  val dataFileWriter:DataFileWriter[GenericRecord] = new DataFileWriter[GenericRecord](datumWriter)
  dataFileWriter.create(schema,file)
  dataFileWriter.append(user1)
  dataFileWriter.append(user2)
  dataFileWriter.close()

}
